
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/tutorial/simplification.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:23:56 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Simplification &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
    <script src="../_static/jquery.js"></script>
    <script src="../_static/underscore.js"></script>
    <script src="../_static/doctools.js"></script>
    <script src="../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../live.sympy.org/static/live-sphinx.js"></script>
    <script async="async" src="../../../cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest8331.js?config=TeX-AMS_HTML-full"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]]}})</script>
    
    <link rel="shortcut icon" href="../_static/sympy-notailtext-favicon.ico"/>
    <link href="simplification.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Calculus" href="calculus.html" />
    <link rel="prev" title="Printing" href="printing.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="calculus.html" title="Calculus"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="printing.html" title="Printing"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">SymPy Tutorial</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Simplification</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="simplification">
<span id="tutorial-simplify"></span><h1>Simplification<a class="headerlink" href="#simplification" title="Permalink to this headline">¶</a></h1>
<p>To make this document easier to read, we are going to enable pretty printing.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y z&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">init_printing</span><span class="p">(</span><span class="n">use_unicode</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<section id="simplify">
<h2><code class="docutils literal notranslate"><span class="pre">simplify</span></code><a class="headerlink" href="#simplify" title="Permalink to this headline">¶</a></h2>
<p>Now let’s jump in and do some interesting mathematics.  One of the most useful
features of a symbolic manipulation system is the ability to simplify
mathematical expressions.  SymPy has dozens of functions to perform various
kinds of simplification.  There is also one general function called
<code class="docutils literal notranslate"><span class="pre">simplify()</span></code> that attempts to apply all of these functions in an intelligent
way to arrive at the simplest form of an expression.  Here are some examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">simplify</span><span class="p">(</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">simplify</span><span class="p">((</span><span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">+</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">x</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span>
<span class="go">x - 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">simplify</span><span class="p">(</span><span class="n">gamma</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="n">gamma</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">2</span><span class="p">))</span>
<span class="go">(x - 2)⋅(x - 1)</span>
</pre></div>
</div>
<p>Here, <code class="docutils literal notranslate"><span class="pre">gamma(x)</span></code> is <span class="math notranslate nohighlight">\(\Gamma(x)\)</span>, the <a class="reference external" href="https://en.wikipedia.org/wiki/Gamma_function">gamma function</a>.  We see that <code class="docutils literal notranslate"><span class="pre">simplify()</span></code>
is capable of handling a large class of expressions.</p>
<p>But <code class="docutils literal notranslate"><span class="pre">simplify()</span></code> has a pitfall.  It just applies all the major
simplification operations in SymPy, and uses heuristics to determine the
simplest result. But “simplest” is not a well-defined term.  For example, say
we wanted to “simplify” <span class="math notranslate nohighlight">\(x^2 + 2x + 1\)</span> into <span class="math notranslate nohighlight">\((x + 1)^2\)</span>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">simplify</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="go"> 2</span>
<span class="go">x  + 2⋅x + 1</span>
</pre></div>
</div>
<p>We did not get what we want.  There is a function to perform this
simplification, called <code class="docutils literal notranslate"><span class="pre">factor()</span></code>, which will be discussed below.</p>
<p>Another pitfall to <code class="docutils literal notranslate"><span class="pre">simplify()</span></code> is that it can be unnecessarily slow, since
it tries many kinds of simplifications before picking the best one.  If you
already know exactly what kind of simplification you are after, it is better
to apply the specific simplification function(s) that apply those
simplifications.</p>
<p>Applying specific simplification functions instead of <code class="docutils literal notranslate"><span class="pre">simplify()</span></code> also has
the advantage that specific functions have certain guarantees about the form
of their output.  These will be discussed with each function below.  For
example, <code class="docutils literal notranslate"><span class="pre">factor()</span></code>, when called on a polynomial with rational coefficients,
is guaranteed to factor the polynomial into irreducible factors.
<code class="docutils literal notranslate"><span class="pre">simplify()</span></code> has no guarantees.  It is entirely heuristical, and, as we saw
above, it may even miss a possible type of simplification that SymPy is
capable of doing.</p>
<p><code class="docutils literal notranslate"><span class="pre">simplify()</span></code> is best when used interactively, when you just want to whittle
down an expression to a simpler form.  You may then choose to apply specific
functions once you see what <code class="docutils literal notranslate"><span class="pre">simplify()</span></code> returns, to get a more precise
result.  It is also useful when you have no idea what form an expression will
take, and you need a catchall function to simplify it.</p>
</section>
<section id="polynomial-rational-function-simplification">
<h2>Polynomial/Rational Function Simplification<a class="headerlink" href="#polynomial-rational-function-simplification" title="Permalink to this headline">¶</a></h2>
<section id="expand">
<h3>expand<a class="headerlink" href="#expand" title="Permalink to this headline">¶</a></h3>
<p><code class="docutils literal notranslate"><span class="pre">expand()</span></code> is one of the most common simplification functions in SymPy.
Although it has a lot of scopes, for now, we will consider its function in
expanding polynomial expressions. For example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expand</span><span class="p">((</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go"> 2</span>
<span class="go">x  + 2⋅x + 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expand</span><span class="p">((</span><span class="n">x</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">3</span><span class="p">))</span>
<span class="go"> 2</span>
<span class="go">x  - x - 6</span>
</pre></div>
</div>
<p>Given a polynomial, <code class="docutils literal notranslate"><span class="pre">expand()</span></code> will put it into a canonical form of a sum of
monomials.</p>
<p><code class="docutils literal notranslate"><span class="pre">expand()</span></code> may not sound like a simplification function.  After all, by its
very name, it makes expressions bigger, not smaller.  Usually this is the
case, but often an expression will become smaller upon calling <code class="docutils literal notranslate"><span class="pre">expand()</span></code> on
it due to cancellation.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expand</span><span class="p">((</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="n">x</span><span class="p">)</span>
<span class="go">-2</span>
</pre></div>
</div>
</section>
<section id="factor">
<h3>factor<a class="headerlink" href="#factor" title="Permalink to this headline">¶</a></h3>
<p><code class="docutils literal notranslate"><span class="pre">factor()</span></code> takes a polynomial and factors it into irreducible factors over
the rational numbers.  For example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">-</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">x</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">        ⎛ 2    ⎞</span>
<span class="go">(x - 1)⋅⎝x  + 1⎠</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">z</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="o">*</span><span class="n">z</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">z</span><span class="p">)</span>
<span class="go">           2</span>
<span class="go">z⋅(x + 2⋅y)</span>
</pre></div>
</div>
<p>For polynomials, <code class="docutils literal notranslate"><span class="pre">factor()</span></code> is the opposite of <code class="docutils literal notranslate"><span class="pre">expand()</span></code>.  <code class="docutils literal notranslate"><span class="pre">factor()</span></code>
uses a complete multivariate factorization algorithm over the rational
numbers, which means that each of the factors returned by <code class="docutils literal notranslate"><span class="pre">factor()</span></code> is
guaranteed to be irreducible.</p>
<p>If you are interested in the factors themselves, <code class="docutils literal notranslate"><span class="pre">factor_list</span></code> returns a
more structured output.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factor_list</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">z</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="o">*</span><span class="n">z</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">z</span><span class="p">)</span>
<span class="go">(1, [(z, 1), (x + 2⋅y, 2)])</span>
</pre></div>
</div>
<p>Note that the input to <code class="docutils literal notranslate"><span class="pre">factor</span></code> and <code class="docutils literal notranslate"><span class="pre">expand</span></code> need not be polynomials in
the strict sense.  They will intelligently factor or expand any kind of
expression (though note that the factors may not be irreducible if the input
is no longer a polynomial over the rationals).</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expand</span><span class="p">((</span><span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">   2                           2</span>
<span class="go">sin (x) + 2⋅sin(x)⋅cos(x) + cos (x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">                 2</span>
<span class="go">(sin(x) + cos(x))</span>
</pre></div>
</div>
</section>
<section id="collect">
<h3>collect<a class="headerlink" href="#collect" title="Permalink to this headline">¶</a></h3>
<p><code class="docutils literal notranslate"><span class="pre">collect()</span></code> collects common powers of a term in an expression.  For example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="n">x</span> <span class="o">-</span> <span class="mi">3</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">z</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">x</span><span class="o">**</span><span class="mi">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go"> 3    2        2</span>
<span class="go">x  - x ⋅z + 2⋅x  + x⋅y + x - 3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collected_expr</span> <span class="o">=</span> <span class="n">collect</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collected_expr</span>
<span class="go"> 3    2</span>
<span class="go">x  + x ⋅(2 - z) + x⋅(y + 1) - 3</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">collect()</span></code> is particularly useful in conjunction with the <code class="docutils literal notranslate"><span class="pre">.coeff()</span></code>
method.  <code class="docutils literal notranslate"><span class="pre">expr.coeff(x,</span> <span class="pre">n)</span></code> gives the coefficient of <code class="docutils literal notranslate"><span class="pre">x**n</span></code> in <code class="docutils literal notranslate"><span class="pre">expr</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">collected_expr</span><span class="o">.</span><span class="n">coeff</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">2 - z</span>
</pre></div>
</div>
</section>
<section id="cancel">
<h3>cancel<a class="headerlink" href="#cancel" title="Permalink to this headline">¶</a></h3>
<p><code class="docutils literal notranslate"><span class="pre">cancel()</span></code> will take any rational function and put it into the standard
canonical form, <span class="math notranslate nohighlight">\(\frac{p}{q}\)</span>, where <span class="math notranslate nohighlight">\(p\)</span> and <span class="math notranslate nohighlight">\(q\)</span> are expanded polynomials with
no common factors, and the leading coefficients of <span class="math notranslate nohighlight">\(p\)</span> and <span class="math notranslate nohighlight">\(q\)</span> do not have
denominators (i.e., are integers).</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">cancel</span><span class="p">((</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">x</span><span class="p">))</span>
<span class="go">x + 1</span>
<span class="go">─────</span>
<span class="go">  x</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="mi">1</span><span class="o">/</span><span class="n">x</span> <span class="o">+</span> <span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="o">/</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">3⋅x</span>
<span class="go">─── - 2</span>
<span class="go"> 2        1</span>
<span class="go">─────── + ─</span>
<span class="go"> x - 4    x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cancel</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
<span class="go">   2</span>
<span class="go">3⋅x  - 2⋅x - 8</span>
<span class="go">──────────────</span>
<span class="go">     2</span>
<span class="go">  2⋅x  - 8⋅x</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="o">*</span><span class="n">z</span> <span class="o">+</span> <span class="n">x</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">y</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">y</span><span class="o">*</span><span class="n">z</span> <span class="o">+</span> <span class="n">z</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">   2                2    2            2</span>
<span class="go">x⋅y  - 2⋅x⋅y⋅z + x⋅z  + y  - 2⋅y⋅z + z</span>
<span class="go">───────────────────────────────────────</span>
<span class="go">                  2</span>
<span class="go">                 x  - 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cancel</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
<span class="go"> 2            2</span>
<span class="go">y  - 2⋅y⋅z + z</span>
<span class="go">───────────────</span>
<span class="go">     x - 1</span>
</pre></div>
</div>
<p>Note that since <code class="docutils literal notranslate"><span class="pre">factor()</span></code> will completely factorize both the numerator and
the denominator of an expression, it can also be used to do the same thing:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factor</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
<span class="go">       2</span>
<span class="go">(y - z)</span>
<span class="go">────────</span>
<span class="go"> x - 1</span>
</pre></div>
</div>
<p>However, if you are only interested in making sure that the expression is in
canceled form, <code class="docutils literal notranslate"><span class="pre">cancel()</span></code> is more efficient than <code class="docutils literal notranslate"><span class="pre">factor()</span></code>.</p>
</section>
<section id="apart">
<h3>apart<a class="headerlink" href="#apart" title="Permalink to this headline">¶</a></h3>
<p><code class="docutils literal notranslate"><span class="pre">apart()</span></code> performs a <a class="reference external" href="https://en.wikipedia.org/wiki/Partial_fraction_decomposition">partial fraction decomposition</a> on a rational
function.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="p">(</span><span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">+</span> <span class="mi">21</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">10</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">12</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">4</span> <span class="o">+</span> <span class="mi">5</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">3</span> <span class="o">+</span> <span class="mi">5</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">   3       2</span>
<span class="go">4⋅x  + 21⋅x  + 10⋅x + 12</span>
<span class="go">────────────────────────</span>
<span class="go">  4      3      2</span>
<span class="go"> x  + 5⋅x  + 5⋅x  + 4⋅x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">apart</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
<span class="go"> 2⋅x - 1       1     3</span>
<span class="go">────────── - ───── + ─</span>
<span class="go"> 2           x + 4   x</span>
<span class="go">x  + x + 1</span>
</pre></div>
</div>
</section>
</section>
<section id="trigonometric-simplification">
<h2>Trigonometric Simplification<a class="headerlink" href="#trigonometric-simplification" title="Permalink to this headline">¶</a></h2>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>SymPy follows Python’s naming conventions for inverse trigonometric
functions, which is to append an <code class="docutils literal notranslate"><span class="pre">a</span></code> to the front of the function’s
name.  For example, the inverse cosine, or arc cosine, is called <code class="docutils literal notranslate"><span class="pre">acos()</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">acos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">acos(x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cos</span><span class="p">(</span><span class="n">acos</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="go">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">asin</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">π</span>
<span class="go">─</span>
<span class="go">2</span>
</pre></div>
</div>
</div>
<section id="trigsimp">
<h3>trigsimp<a class="headerlink" href="#trigsimp" title="Permalink to this headline">¶</a></h3>
<p>To simplify expressions using trigonometric identities, use <code class="docutils literal notranslate"><span class="pre">trigsimp()</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">trigsimp</span><span class="p">(</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">trigsimp</span><span class="p">(</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">4</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">4</span><span class="p">)</span>
<span class="go">cos(4⋅x)   1</span>
<span class="go">──────── + ─</span>
<span class="go">   2       2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">trigsimp</span><span class="p">(</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">tan</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="n">sec</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="go">   2</span>
<span class="go">sin (x)</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">trigsimp()</span></code> also works with hyperbolic trig functions.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">trigsimp</span><span class="p">(</span><span class="n">cosh</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">sinh</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">cosh(2⋅x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">trigsimp</span><span class="p">(</span><span class="n">sinh</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="n">tanh</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="go">cosh(x)</span>
</pre></div>
</div>
<p>Much like <code class="docutils literal notranslate"><span class="pre">simplify()</span></code>, <code class="docutils literal notranslate"><span class="pre">trigsimp()</span></code> applies various trigonometric identities to
the input expression, and then uses a heuristic to return the “best” one.</p>
</section>
<section id="expand-trig">
<h3>expand_trig<a class="headerlink" href="#expand-trig" title="Permalink to this headline">¶</a></h3>
<p>To expand trigonometric functions, that is, apply the sum or double angle
identities, use <code class="docutils literal notranslate"><span class="pre">expand_trig()</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expand_trig</span><span class="p">(</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">))</span>
<span class="go">sin(x)⋅cos(y) + sin(y)⋅cos(x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expand_trig</span><span class="p">(</span><span class="n">tan</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">))</span>
<span class="go">  2⋅tan(x)</span>
<span class="go">───────────</span>
<span class="go">       2</span>
<span class="go">1 - tan (x)</span>
</pre></div>
</div>
<p>Because <code class="docutils literal notranslate"><span class="pre">expand_trig()</span></code> tends to make trigonometric expressions larger, and
<code class="docutils literal notranslate"><span class="pre">trigsimp()</span></code> tends to make them smaller, these identities can be applied in
reverse using <code class="docutils literal notranslate"><span class="pre">trigsimp()</span></code></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">trigsimp</span><span class="p">(</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">cos</span><span class="p">(</span><span class="n">y</span><span class="p">)</span> <span class="o">+</span> <span class="n">sin</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="o">*</span><span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="go">sin(x + y)</span>
</pre></div>
</div>
</section>
</section>
<section id="powers">
<h2>Powers<a class="headerlink" href="#powers" title="Permalink to this headline">¶</a></h2>
<p>Before we introduce the power simplification functions, a mathematical
discussion on the identities held by powers is in order.  There are three
kinds of identities satisfied by exponents</p>
<ol class="arabic simple">
<li><p><span class="math notranslate nohighlight">\(x^ax^b = x^{a + b}\)</span></p></li>
<li><p><span class="math notranslate nohighlight">\(x^ay^a = (xy)^a\)</span></p></li>
<li><p><span class="math notranslate nohighlight">\((x^a)^b = x^{ab}\)</span></p></li>
</ol>
<p>Identity 1 is always true.</p>
<p>Identity 2 is not always true.  For example, if <span class="math notranslate nohighlight">\(x = y = -1\)</span> and <span class="math notranslate nohighlight">\(a =
\frac{1}{2}\)</span>, then <span class="math notranslate nohighlight">\(x^ay^a = \sqrt{-1}\sqrt{-1} = i\cdot i = -1\)</span>, whereas
<span class="math notranslate nohighlight">\((xy)^a = \sqrt{-1\cdot-1} = \sqrt{1} = 1\)</span>.  However, identity 2 is true at
least if <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(y\)</span> are nonnegative and <span class="math notranslate nohighlight">\(a\)</span> is real (it may also be true
under other conditions as well).  A common consequence of the failure of
identity 2 is that <span class="math notranslate nohighlight">\(\sqrt{x}\sqrt{y} \neq \sqrt{xy}\)</span>.</p>
<p>Identity 3 is not always true.  For example, if <span class="math notranslate nohighlight">\(x = -1\)</span>, <span class="math notranslate nohighlight">\(a = 2\)</span>, and <span class="math notranslate nohighlight">\(b =
\frac{1}{2}\)</span>, then <span class="math notranslate nohighlight">\((x^a)^b = {\left((-1)^2\right)}^{1/2} = \sqrt{1} = 1\)</span>
and <span class="math notranslate nohighlight">\(x^{ab} = (-1)^{2\cdot1/2} = (-1)^1 = -1\)</span>.  However, identity 3 is true
when <span class="math notranslate nohighlight">\(b\)</span> is an integer (again, it may also hold in other cases as well).  Two
common consequences of the failure of identity 3 are that <span class="math notranslate nohighlight">\(\sqrt{x^2}\neq x\)</span>
and that <span class="math notranslate nohighlight">\(\sqrt{\frac{1}{x}} \neq \frac{1}{\sqrt{x}}\)</span>.</p>
<p>To summarize</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 12%" />
<col style="width: 19%" />
<col style="width: 28%" />
<col style="width: 41%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Identity</p></th>
<th class="head"><p>Sufficient conditions to hold</p></th>
<th class="head"><p>Counterexample when conditions are not met</p></th>
<th class="head"><p>Important consequences</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><ol class="arabic simple">
<li><p><span class="math notranslate nohighlight">\(x^ax^b = x^{a + b}\)</span></p></li>
</ol>
</td>
<td><p>Always true</p></td>
<td><p>None</p></td>
<td><p>None</p></td>
</tr>
<tr class="row-odd"><td><ol class="arabic simple" start="2">
<li><p><span class="math notranslate nohighlight">\(x^ay^a = (xy)^a\)</span></p></li>
</ol>
</td>
<td><p><span class="math notranslate nohighlight">\(x, y \geq 0\)</span> and <span class="math notranslate nohighlight">\(a \in \mathbb{R}\)</span></p></td>
<td><p><span class="math notranslate nohighlight">\((-1)^{1/2}(-1)^{1/2} \neq (-1\cdot-1)^{1/2}\)</span></p></td>
<td><p><span class="math notranslate nohighlight">\(\sqrt{x}\sqrt{y} \neq \sqrt{xy}\)</span> in general</p></td>
</tr>
<tr class="row-even"><td><ol class="arabic simple" start="3">
<li><p><span class="math notranslate nohighlight">\((x^a)^b = x^{ab}\)</span></p></li>
</ol>
</td>
<td><p><span class="math notranslate nohighlight">\(b \in \mathbb{Z}\)</span></p></td>
<td><p><span class="math notranslate nohighlight">\({\left((-1)^2\right)}^{1/2} \neq (-1)^{2\cdot1/2}\)</span></p></td>
<td><p><span class="math notranslate nohighlight">\(\sqrt{x^2}\neq x\)</span> and <span class="math notranslate nohighlight">\(\sqrt{\frac{1}{x}}\neq\frac{1}{\sqrt{x}}\)</span> in general</p></td>
</tr>
</tbody>
</table>
<p>This is important to remember, because by default, SymPy will not perform
simplifications if they are not true in general.</p>
<p>In order to make SymPy perform simplifications involving identities that are
only true under certain assumptions, we need to put assumptions on our
Symbols.  We will undertake a full discussion of the assumptions system later,
but for now, all we need to know are the following.</p>
<ul>
<li><p>By default, SymPy Symbols are assumed to be complex (elements of
<span class="math notranslate nohighlight">\(\mathbb{C}\)</span>).  That is, a simplification will not be applied to an
expression with a given Symbol unless it holds for all complex numbers.</p></li>
<li><p>Symbols can be given different assumptions by passing the assumption to
<code class="docutils literal notranslate"><span class="pre">symbols()</span></code>.  For the rest of this section, we will be assuming that <code class="docutils literal notranslate"><span class="pre">x</span></code>
and <code class="docutils literal notranslate"><span class="pre">y</span></code> are positive, and that <code class="docutils literal notranslate"><span class="pre">a</span></code> and <code class="docutils literal notranslate"><span class="pre">b</span></code> are real.  We will leave
<code class="docutils literal notranslate"><span class="pre">z</span></code>, <code class="docutils literal notranslate"><span class="pre">t</span></code>, and <code class="docutils literal notranslate"><span class="pre">c</span></code> as arbitrary complex Symbols to demonstrate what
happens in that case.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;a b&#39;</span><span class="p">,</span> <span class="n">real</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">c</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;z t c&#39;</span><span class="p">)</span>
</pre></div>
</div>
</li>
</ul>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>In SymPy, <code class="docutils literal notranslate"><span class="pre">sqrt(x)</span></code> is just a shortcut to <code class="docutils literal notranslate"><span class="pre">x**Rational(1,</span> <span class="pre">2)</span></code>.  They
are exactly the same object.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">==</span> <span class="n">x</span><span class="o">**</span><span class="n">Rational</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<section id="powsimp">
<h3>powsimp<a class="headerlink" href="#powsimp" title="Permalink to this headline">¶</a></h3>
<p><code class="docutils literal notranslate"><span class="pre">powsimp()</span></code> applies identities 1 and 2 from above, from left to right.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">a</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">b</span><span class="p">)</span>
<span class="go">  a + b</span>
<span class="go"> x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">a</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">a</span><span class="p">)</span>
<span class="go">     a</span>
<span class="go">(x⋅y)</span>
</pre></div>
</div>
<p>Notice that <code class="docutils literal notranslate"><span class="pre">powsimp()</span></code> refuses to do the simplification if it is not valid.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">t</span><span class="o">**</span><span class="n">c</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="n">c</span><span class="p">)</span>
<span class="go"> c  c</span>
<span class="go">t ⋅z</span>
</pre></div>
</div>
<p>If you know that you want to apply this simplification, but you don’t want to
mess with assumptions, you can pass the <code class="docutils literal notranslate"><span class="pre">force=True</span></code> flag.  This will force
the simplification to take place, regardless of assumptions.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">t</span><span class="o">**</span><span class="n">c</span><span class="o">*</span><span class="n">z</span><span class="o">**</span><span class="n">c</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">     c</span>
<span class="go">(t⋅z)</span>
</pre></div>
</div>
<p>Note that in some instances, in particular, when the exponents are integers or
rational numbers, and identity 2 holds, it will be applied automatically.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">z</span><span class="o">*</span><span class="n">t</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span>
<span class="go">  2  2</span>
<span class="go"> t ⋅z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">)</span>
<span class="go"> √x⋅√y</span>
</pre></div>
</div>
<p>This means that it will be impossible to undo this identity with
<code class="docutils literal notranslate"><span class="pre">powsimp()</span></code>, because even if <code class="docutils literal notranslate"><span class="pre">powsimp()</span></code> were to put the bases together,
they would be automatically split apart again.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">z</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">t</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">  2  2</span>
<span class="go"> t ⋅z</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">powsimp</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="n">y</span><span class="p">))</span>
<span class="go"> √x⋅√y</span>
</pre></div>
</div>
</section>
<section id="expand-power-exp-expand-power-base">
<h3>expand_power_exp / expand_power_base<a class="headerlink" href="#expand-power-exp-expand-power-base" title="Permalink to this headline">¶</a></h3>
<p><code class="docutils literal notranslate"><span class="pre">expand_power_exp()</span></code> and <code class="docutils literal notranslate"><span class="pre">expand_power_base()</span></code> apply identities 1 and 2
from right to left, respectively.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expand_power_exp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">))</span>
<span class="go"> a  b</span>
<span class="go">x ⋅x</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expand_power_base</span><span class="p">((</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">)</span><span class="o">**</span><span class="n">a</span><span class="p">)</span>
<span class="go"> a  a</span>
<span class="go">x ⋅y</span>
</pre></div>
</div>
<p>As with <code class="docutils literal notranslate"><span class="pre">powsimp()</span></code>, identity 2 is not applied if it is not valid.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expand_power_base</span><span class="p">((</span><span class="n">z</span><span class="o">*</span><span class="n">t</span><span class="p">)</span><span class="o">**</span><span class="n">c</span><span class="p">)</span>
<span class="go">     c</span>
<span class="go">(t⋅z)</span>
</pre></div>
</div>
<p>And as with <code class="docutils literal notranslate"><span class="pre">powsimp()</span></code>, you can force the expansion to happen without
fiddling with assumptions by using <code class="docutils literal notranslate"><span class="pre">force=True</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expand_power_base</span><span class="p">((</span><span class="n">z</span><span class="o">*</span><span class="n">t</span><span class="p">)</span><span class="o">**</span><span class="n">c</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">  c  c</span>
<span class="go"> t ⋅z</span>
</pre></div>
</div>
<p>As with identity 2, identity 1 is applied automatically if the power is a
number, and hence cannot be undone with <code class="docutils literal notranslate"><span class="pre">expand_power_exp()</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">3</span>
<span class="go">  5</span>
<span class="go"> x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expand_power_exp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">5</span><span class="p">)</span>
<span class="go">  5</span>
<span class="go"> x</span>
</pre></div>
</div>
</section>
<section id="powdenest">
<h3>powdenest<a class="headerlink" href="#powdenest" title="Permalink to this headline">¶</a></h3>
<p><code class="docutils literal notranslate"><span class="pre">powdenest()</span></code> applies identity 3, from left to right.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">powdenest</span><span class="p">((</span><span class="n">x</span><span class="o">**</span><span class="n">a</span><span class="p">)</span><span class="o">**</span><span class="n">b</span><span class="p">)</span>
<span class="go"> a⋅b</span>
<span class="go">x</span>
</pre></div>
</div>
<p>As before, the identity is not applied if it is not true under the given
assumptions.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">powdenest</span><span class="p">((</span><span class="n">z</span><span class="o">**</span><span class="n">a</span><span class="p">)</span><span class="o">**</span><span class="n">b</span><span class="p">)</span>
<span class="go">    b</span>
<span class="go">⎛ a⎞</span>
<span class="go">⎝z ⎠</span>
</pre></div>
</div>
<p>And as before, this can be manually overridden with <code class="docutils literal notranslate"><span class="pre">force=True</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">powdenest</span><span class="p">((</span><span class="n">z</span><span class="o">**</span><span class="n">a</span><span class="p">)</span><span class="o">**</span><span class="n">b</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go"> a⋅b</span>
<span class="go">z</span>
</pre></div>
</div>
</section>
</section>
<section id="exponentials-and-logarithms">
<h2>Exponentials and logarithms<a class="headerlink" href="#exponentials-and-logarithms" title="Permalink to this headline">¶</a></h2>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>In SymPy, as in Python and most programming languages, <code class="docutils literal notranslate"><span class="pre">log</span></code> is the
natural logarithm, also known as <code class="docutils literal notranslate"><span class="pre">ln</span></code>.  SymPy automatically provides an
alias <code class="docutils literal notranslate"><span class="pre">ln</span> <span class="pre">=</span> <span class="pre">log</span></code> in case you forget this.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ln</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">log(x)</span>
</pre></div>
</div>
</div>
<p>Logarithms have similar issues as powers.  There are two main identities</p>
<ol class="arabic simple">
<li><p><span class="math notranslate nohighlight">\(\log{(xy)} = \log{(x)} + \log{(y)}\)</span></p></li>
<li><p><span class="math notranslate nohighlight">\(\log{(x^n)} = n\log{(x)}\)</span></p></li>
</ol>
<p>Neither identity is true for arbitrary complex <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(y\)</span>, due to the branch
cut in the complex plane for the complex logarithm.  However, sufficient
conditions for the identities to hold are if <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(y\)</span> are positive and <span class="math notranslate nohighlight">\(n\)</span>
is real.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;n&#39;</span><span class="p">,</span> <span class="n">real</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>As before, <code class="docutils literal notranslate"><span class="pre">z</span></code> and <code class="docutils literal notranslate"><span class="pre">t</span></code> will be Symbols with no additional assumptions.</p>
<p>Note that the identity <span class="math notranslate nohighlight">\(\log{\left(\frac{x}{y}\right)} = \log(x) - \log(y)\)</span>
is a special case of identities 1 and 2 by <span class="math notranslate nohighlight">\(\log{\left(\frac{x}{y}\right)}
=\)</span> <span class="math notranslate nohighlight">\(\log{\left(x\cdot\frac{1}{y}\right)} =\)</span> <span class="math notranslate nohighlight">\(\log(x) + \log{\left(
y^{-1}\right)} =\)</span> <span class="math notranslate nohighlight">\(\log(x) - \log(y)\)</span>, and thus it also holds if <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(y\)</span>
are positive, but may not hold in general.</p>
<p>We also see that <span class="math notranslate nohighlight">\(\log{\left( e^x \right)} = x\)</span> comes from <span class="math notranslate nohighlight">\(\log{\left( e^x
\right)} = x\log(e) = x\)</span>, and thus holds when <span class="math notranslate nohighlight">\(x\)</span> is real (and it can be
verified that it does not hold in general for arbitrary complex <span class="math notranslate nohighlight">\(x\)</span>, for
example, <span class="math notranslate nohighlight">\(\log{\left(e^{x + 2\pi i}\right)} = \log{\left(e^x\right)} = x
\neq x + 2\pi i\)</span>).</p>
<section id="expand-log">
<h3>expand_log<a class="headerlink" href="#expand-log" title="Permalink to this headline">¶</a></h3>
<p>To apply identities 1 and 2 from left to right, use <code class="docutils literal notranslate"><span class="pre">expand_log()</span></code>.  As
always, the identities will not be applied unless they are valid.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expand_log</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">))</span>
<span class="go">log(x) + log(y)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expand_log</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">x</span><span class="o">/</span><span class="n">y</span><span class="p">))</span>
<span class="go">log(x) - log(y)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expand_log</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
<span class="go">2⋅log(x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expand_log</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">n</span><span class="p">))</span>
<span class="go">n⋅log(x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expand_log</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">z</span><span class="o">*</span><span class="n">t</span><span class="p">))</span>
<span class="go">log(t⋅z)</span>
</pre></div>
</div>
<p>As with <code class="docutils literal notranslate"><span class="pre">powsimp()</span></code> and <code class="docutils literal notranslate"><span class="pre">powdenest()</span></code>, <code class="docutils literal notranslate"><span class="pre">expand_log()</span></code> has a <code class="docutils literal notranslate"><span class="pre">force</span></code>
option that can be used to ignore assumptions.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expand_log</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">z</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
<span class="go">   ⎛ 2⎞</span>
<span class="go">log⎝z ⎠</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expand_log</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">z</span><span class="o">**</span><span class="mi">2</span><span class="p">),</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">2⋅log(z)</span>
</pre></div>
</div>
</section>
<section id="logcombine">
<h3>logcombine<a class="headerlink" href="#logcombine" title="Permalink to this headline">¶</a></h3>
<p>To apply identities 1 and 2 from right to left, use <code class="docutils literal notranslate"><span class="pre">logcombine()</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">logcombine</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">+</span> <span class="n">log</span><span class="p">(</span><span class="n">y</span><span class="p">))</span>
<span class="go">log(x⋅y)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">logcombine</span><span class="p">(</span><span class="n">n</span><span class="o">*</span><span class="n">log</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="go">   ⎛ n⎞</span>
<span class="go">log⎝x ⎠</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">logcombine</span><span class="p">(</span><span class="n">n</span><span class="o">*</span><span class="n">log</span><span class="p">(</span><span class="n">z</span><span class="p">))</span>
<span class="go">n⋅log(z)</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">logcombine()</span></code> also has a <code class="docutils literal notranslate"><span class="pre">force</span></code> option that can be used to ignore
assumptions.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">logcombine</span><span class="p">(</span><span class="n">n</span><span class="o">*</span><span class="n">log</span><span class="p">(</span><span class="n">z</span><span class="p">),</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">   ⎛ n⎞</span>
<span class="go">log⎝z ⎠</span>
</pre></div>
</div>
</section>
</section>
<section id="special-functions">
<h2>Special Functions<a class="headerlink" href="#special-functions" title="Permalink to this headline">¶</a></h2>
<p>SymPy implements dozens of special functions, ranging from functions in
combinatorics to mathematical physics.</p>
<p>An extensive list of the special functions included with SymPy and their
documentation is at the <a class="reference internal" href="../modules/functions/index.html#functions-contents"><span class="std std-ref">Functions Module</span></a> page.</p>
<p>For the purposes of this tutorial, let’s introduce a few special functions in
SymPy.</p>
<p>Let’s define <code class="docutils literal notranslate"><span class="pre">x</span></code>, <code class="docutils literal notranslate"><span class="pre">y</span></code>, and <code class="docutils literal notranslate"><span class="pre">z</span></code> as regular, complex Symbols, removing any
assumptions we put on them in the previous section.  We will also define <code class="docutils literal notranslate"><span class="pre">k</span></code>,
<code class="docutils literal notranslate"><span class="pre">m</span></code>, and <code class="docutils literal notranslate"><span class="pre">n</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y z&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">k</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">n</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;k m n&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>The <a class="reference external" href="https://en.wikipedia.org/wiki/Factorial">factorial</a> function is
<code class="docutils literal notranslate"><span class="pre">factorial</span></code>.  <code class="docutils literal notranslate"><span class="pre">factorial(n)</span></code> represents <span class="math notranslate nohighlight">\(n!= 1\cdot2\cdots(n - 1)\cdot
n\)</span>. <span class="math notranslate nohighlight">\(n!\)</span> represents the number of permutations of <span class="math notranslate nohighlight">\(n\)</span> distinct items.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factorial</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="go">n!</span>
</pre></div>
</div>
<p>The <a class="reference external" href="https://en.wikipedia.org/wiki/Binomial_coefficient">binomial coefficient</a> function is
<code class="docutils literal notranslate"><span class="pre">binomial</span></code>.  <code class="docutils literal notranslate"><span class="pre">binomial(n,</span> <span class="pre">k)</span></code> represents <span class="math notranslate nohighlight">\(\binom{n}{k}\)</span>, the number of
ways to choose <span class="math notranslate nohighlight">\(k\)</span> items from a set of <span class="math notranslate nohighlight">\(n\)</span> distinct items.  It is also often
written as <span class="math notranslate nohighlight">\(nCk\)</span>, and is pronounced “<span class="math notranslate nohighlight">\(n\)</span> choose <span class="math notranslate nohighlight">\(k\)</span>”.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">binomial</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">k</span><span class="p">)</span>
<span class="go">⎛n⎞</span>
<span class="go">⎜ ⎟</span>
<span class="go">⎝k⎠</span>
</pre></div>
</div>
<p>The factorial function is closely related to the <a class="reference external" href="https://en.wikipedia.org/wiki/Gamma_function">gamma function</a>, <code class="docutils literal notranslate"><span class="pre">gamma</span></code>.  <code class="docutils literal notranslate"><span class="pre">gamma(z)</span></code>
represents <span class="math notranslate nohighlight">\(\Gamma(z) = \int_0^\infty t^{z - 1}e^{-t}\,dt\)</span>, which for positive integer
<span class="math notranslate nohighlight">\(z\)</span> is the same as <span class="math notranslate nohighlight">\((z - 1)!\)</span>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">gamma</span><span class="p">(</span><span class="n">z</span><span class="p">)</span>
<span class="go">Γ(z)</span>
</pre></div>
</div>
<p>The <a class="reference external" href="https://en.wikipedia.org/wiki/Generalized_hypergeometric_function">generalized hypergeometric function</a> is
<code class="docutils literal notranslate"><span class="pre">hyper</span></code>.  <code class="docutils literal notranslate"><span class="pre">hyper([a_1,</span> <span class="pre">...,</span> <span class="pre">a_p],</span> <span class="pre">[b_1,</span> <span class="pre">...,</span> <span class="pre">b_q],</span> <span class="pre">z)</span></code> represents
<span class="math notranslate nohighlight">\({}_pF_q\left(\begin{matrix} a_1, \cdots, a_p \\ b_1, \cdots, b_q \end{matrix}
\middle| z \right)\)</span>.  The most common case is <span class="math notranslate nohighlight">\({}_2F_1\)</span>, which is often
referred to as the <a class="reference external" href="https://en.wikipedia.org/wiki/Hypergeometric_function">ordinary hypergeometric function</a>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">hyper</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="n">z</span><span class="p">)</span>
<span class="go"> ┌─  ⎛1, 2 │  ⎞</span>
<span class="go"> ├─  ⎜     │ z⎟</span>
<span class="go">2╵ 1 ⎝ 3   │  ⎠</span>
</pre></div>
</div>
<section id="rewrite">
<h3>rewrite<a class="headerlink" href="#rewrite" title="Permalink to this headline">¶</a></h3>
<p>A common way to deal with special functions is to rewrite them in terms of one
another.  This works for any function in SymPy, not just special functions.
To rewrite an expression in terms of a function, use
<code class="docutils literal notranslate"><span class="pre">expr.rewrite(function)</span></code>.  For example,</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">tan</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">rewrite</span><span class="p">(</span><span class="n">sin</span><span class="p">)</span>
<span class="go">     2</span>
<span class="go">2⋅sin (x)</span>
<span class="go">─────────</span>
<span class="go"> sin(2⋅x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">factorial</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">rewrite</span><span class="p">(</span><span class="n">gamma</span><span class="p">)</span>
<span class="go">Γ(x + 1)</span>
</pre></div>
</div>
<p>For some tips on applying more targeted rewriting, see the
<a class="reference internal" href="manipulation.html#tutorial-manipulation"><span class="std std-ref">Advanced Expression Manipulation</span></a> section.</p>
</section>
<section id="expand-func">
<h3>expand_func<a class="headerlink" href="#expand-func" title="Permalink to this headline">¶</a></h3>
<p>To expand special functions in terms of some identities, use
<code class="docutils literal notranslate"><span class="pre">expand_func()</span></code>.  For example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expand_func</span><span class="p">(</span><span class="n">gamma</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">3</span><span class="p">))</span>
<span class="go">x⋅(x + 1)⋅(x + 2)⋅Γ(x)</span>
</pre></div>
</div>
</section>
<section id="hyperexpand">
<h3>hyperexpand<a class="headerlink" href="#hyperexpand" title="Permalink to this headline">¶</a></h3>
<p>To rewrite <code class="docutils literal notranslate"><span class="pre">hyper</span></code> in terms of more standard functions, use
<code class="docutils literal notranslate"><span class="pre">hyperexpand()</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">hyperexpand</span><span class="p">(</span><span class="n">hyper</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">z</span><span class="p">))</span>
<span class="go">-log(1 - z)</span>
<span class="go">────────────</span>
<span class="go">     z</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">hyperexpand()</span></code> also works on the more general Meijer G-function (see
<a class="reference internal" href="../modules/functions/special.html#sympy.functions.special.hyper.meijerg" title="sympy.functions.special.hyper.meijerg"><code class="xref py py-meth docutils literal notranslate"><span class="pre">its</span> <span class="pre">documentation</span></code></a> for more
information).</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">meijerg</span><span class="p">([[</span><span class="mi">1</span><span class="p">],[</span><span class="mi">1</span><span class="p">]],</span> <span class="p">[[</span><span class="mi">1</span><span class="p">],[]],</span> <span class="o">-</span><span class="n">z</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">╭─╮1, 1 ⎛1  1 │   ⎞</span>
<span class="go">│╶┐     ⎜     │ -z⎟</span>
<span class="go">╰─╯2, 1 ⎝1    │   ⎠</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">hyperexpand</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
<span class="go"> 1</span>
<span class="go"> ─</span>
<span class="go"> z</span>
<span class="go">ℯ</span>
</pre></div>
</div>
</section>
<section id="combsimp">
<h3>combsimp<a class="headerlink" href="#combsimp" title="Permalink to this headline">¶</a></h3>
<p>To simplify combinatorial expressions, use <code class="docutils literal notranslate"><span class="pre">combsimp()</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span><span class="p">,</span> <span class="n">k</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;n k&#39;</span><span class="p">,</span> <span class="n">integer</span> <span class="o">=</span> <span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">combsimp</span><span class="p">(</span><span class="n">factorial</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="o">/</span><span class="n">factorial</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">3</span><span class="p">))</span>
<span class="go">n⋅(n - 2)⋅(n - 1)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">combsimp</span><span class="p">(</span><span class="n">binomial</span><span class="p">(</span><span class="n">n</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="n">binomial</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">k</span><span class="p">))</span>
<span class="go">n + 1</span>
<span class="go">─────</span>
<span class="go">k + 1</span>
</pre></div>
</div>
</section>
<section id="gammasimp">
<h3>gammasimp<a class="headerlink" href="#gammasimp" title="Permalink to this headline">¶</a></h3>
<p>To simplify expressions with gamma functions or combinatorial functions with
non-integer argument, use <code class="docutils literal notranslate"><span class="pre">gammasimp()</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">gammasimp</span><span class="p">(</span><span class="n">gamma</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">gamma</span><span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">x</span><span class="p">))</span>
<span class="go">   π</span>
<span class="go">────────</span>
<span class="go">sin(π⋅x)</span>
</pre></div>
</div>
</section>
</section>
<section id="example-continued-fractions">
<h2>Example: Continued Fractions<a class="headerlink" href="#example-continued-fractions" title="Permalink to this headline">¶</a></h2>
<p>Let’s use SymPy to explore continued fractions.  A <a class="reference external" href="https://en.wikipedia.org/wiki/Continued_fraction">continued fraction</a> is an expression of the
form</p>
<div class="math notranslate nohighlight">
\[a_0 + \cfrac{1}{a_1 + \cfrac{1}{a_2 + \cfrac{1}{ \ddots + \cfrac{1}{a_n}
}}}\]</div>
<p>where <span class="math notranslate nohighlight">\(a_0, \ldots, a_n\)</span> are integers, and <span class="math notranslate nohighlight">\(a_1, \ldots, a_n\)</span> are positive. A
continued fraction can also be infinite, but infinite objects are more
difficult to represent in computers, so we will only examine the finite case
here.</p>
<p>A continued fraction of the above form is often represented as a list <span class="math notranslate nohighlight">\([a_0;
a_1, \ldots, a_n]\)</span>.  Let’s write a simple function that converts such a list
to its continued fraction form.  The easiest way to construct a continued
fraction from a list is to work backwards.  Note that despite the apparent
symmetry of the definition, the first element, <span class="math notranslate nohighlight">\(a_0\)</span>, must usually be handled
differently from the rest.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">list_to_frac</span><span class="p">(</span><span class="n">l</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">expr</span> <span class="o">=</span> <span class="n">Integer</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">l</span><span class="p">[</span><span class="mi">1</span><span class="p">:]):</span>
<span class="gp">... </span>        <span class="n">expr</span> <span class="o">+=</span> <span class="n">i</span>
<span class="gp">... </span>        <span class="n">expr</span> <span class="o">=</span> <span class="mi">1</span><span class="o">/</span><span class="n">expr</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="n">l</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">expr</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">list_to_frac</span><span class="p">([</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">])</span>
<span class="go">      1</span>
<span class="go">x + ─────</span>
<span class="go">        1</span>
<span class="go">    y + ─</span>
<span class="go">        z</span>
</pre></div>
</div>
<p>We use <code class="docutils literal notranslate"><span class="pre">Integer(0)</span></code> in <code class="docutils literal notranslate"><span class="pre">list_to_frac</span></code> so that the result will always be a
SymPy object, even if we only pass in Python ints.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">list_to_frac</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">])</span>
<span class="go">43</span>
<span class="go">──</span>
<span class="go">30</span>
</pre></div>
</div>
<p>Every finite continued fraction is a rational number, but we are interested in
symbolics here, so let’s create a symbolic continued fraction.  The
<code class="docutils literal notranslate"><span class="pre">symbols()</span></code> function that we have been using has a shortcut to create
numbered symbols.  <code class="docutils literal notranslate"><span class="pre">symbols('a0:5')</span></code> will create the symbols <code class="docutils literal notranslate"><span class="pre">a0</span></code>, <code class="docutils literal notranslate"><span class="pre">a1</span></code>,
…, <code class="docutils literal notranslate"><span class="pre">a4</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">syms</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;a0:5&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">syms</span>
<span class="go">(a₀, a₁, a₂, a₃, a₄)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a0</span><span class="p">,</span> <span class="n">a1</span><span class="p">,</span> <span class="n">a2</span><span class="p">,</span> <span class="n">a3</span><span class="p">,</span> <span class="n">a4</span> <span class="o">=</span> <span class="n">syms</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span> <span class="o">=</span> <span class="n">list_to_frac</span><span class="p">(</span><span class="n">syms</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span>
<span class="go">             1</span>
<span class="go">a₀ + ─────────────────</span>
<span class="go">               1</span>
<span class="go">     a₁ + ────────────</span>
<span class="go">                  1</span>
<span class="go">          a₂ + ───────</span>
<span class="go">                    1</span>
<span class="go">               a₃ + ──</span>
<span class="go">                    a₄</span>
</pre></div>
</div>
<p>This form is useful for understanding continued fractions, but lets put it
into standard rational function form using <code class="docutils literal notranslate"><span class="pre">cancel()</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span> <span class="o">=</span> <span class="n">cancel</span><span class="p">(</span><span class="n">frac</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span>
<span class="go">a₀⋅a₁⋅a₂⋅a₃⋅a₄ + a₀⋅a₁⋅a₂ + a₀⋅a₁⋅a₄ + a₀⋅a₃⋅a₄ + a₀ + a₂⋅a₃⋅a₄ + a₂ + a₄</span>
<span class="go">─────────────────────────────────────────────────────────────────────────</span>
<span class="go">                 a₁⋅a₂⋅a₃⋅a₄ + a₁⋅a₂ + a₁⋅a₄ + a₃⋅a₄ + 1</span>
</pre></div>
</div>
<p>Now suppose we were given <code class="docutils literal notranslate"><span class="pre">frac</span></code> in the above canceled form. In fact, we
might be given the fraction in any form, but we can always put it into the
above canonical form with <code class="docutils literal notranslate"><span class="pre">cancel()</span></code>.  Suppose that we knew that it could be
rewritten as a continued fraction.  How could we do this with SymPy?  A
continued fraction is recursively <span class="math notranslate nohighlight">\(c + \frac{1}{f}\)</span>, where <span class="math notranslate nohighlight">\(c\)</span> is an integer
and <span class="math notranslate nohighlight">\(f\)</span> is a (smaller) continued fraction.  If we could write the expression
in this form, we could pull out each <span class="math notranslate nohighlight">\(c\)</span> recursively and add it to a list.  We
could then get a continued fraction with our <code class="docutils literal notranslate"><span class="pre">list_to_frac()</span></code> function.</p>
<p>The key observation here is that we can convert an expression to the form <span class="math notranslate nohighlight">\(c +
\frac{1}{f}\)</span> by doing a partial fraction decomposition with respect to
<span class="math notranslate nohighlight">\(c\)</span>. This is because <span class="math notranslate nohighlight">\(f\)</span> does not contain <span class="math notranslate nohighlight">\(c\)</span>.  This means we need to use the
<code class="docutils literal notranslate"><span class="pre">apart()</span></code> function.  We use <code class="docutils literal notranslate"><span class="pre">apart()</span></code> to pull the term out, then subtract
it from the expression, and take the reciprocal to get the <span class="math notranslate nohighlight">\(f\)</span> part.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">l</span> <span class="o">=</span> <span class="p">[]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span> <span class="o">=</span> <span class="n">apart</span><span class="p">(</span><span class="n">frac</span><span class="p">,</span> <span class="n">a0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span>
<span class="go">                a₂⋅a₃⋅a₄ + a₂ + a₄</span>
<span class="go">a₀ + ───────────────────────────────────────</span>
<span class="go">     a₁⋅a₂⋅a₃⋅a₄ + a₁⋅a₂ + a₁⋅a₄ + a₃⋅a₄ + 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">l</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">a0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span> <span class="o">=</span> <span class="mi">1</span><span class="o">/</span><span class="p">(</span><span class="n">frac</span> <span class="o">-</span> <span class="n">a0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span>
<span class="go">a₁⋅a₂⋅a₃⋅a₄ + a₁⋅a₂ + a₁⋅a₄ + a₃⋅a₄ + 1</span>
<span class="go">───────────────────────────────────────</span>
<span class="go">           a₂⋅a₃⋅a₄ + a₂ + a₄</span>
</pre></div>
</div>
<p>Now we repeat this process</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span> <span class="o">=</span> <span class="n">apart</span><span class="p">(</span><span class="n">frac</span><span class="p">,</span> <span class="n">a1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span>
<span class="go">         a₃⋅a₄ + 1</span>
<span class="go">a₁ + ──────────────────</span>
<span class="go">     a₂⋅a₃⋅a₄ + a₂ + a₄</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">l</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">a1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span> <span class="o">=</span> <span class="mi">1</span><span class="o">/</span><span class="p">(</span><span class="n">frac</span> <span class="o">-</span> <span class="n">a1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span> <span class="o">=</span> <span class="n">apart</span><span class="p">(</span><span class="n">frac</span><span class="p">,</span> <span class="n">a2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span>
<span class="go">         a₄</span>
<span class="go">a₂ + ─────────</span>
<span class="go">     a₃⋅a₄ + 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">l</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">a2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span> <span class="o">=</span> <span class="mi">1</span><span class="o">/</span><span class="p">(</span><span class="n">frac</span> <span class="o">-</span> <span class="n">a2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span> <span class="o">=</span> <span class="n">apart</span><span class="p">(</span><span class="n">frac</span><span class="p">,</span> <span class="n">a3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span>
<span class="go">     1</span>
<span class="go">a₃ + ──</span>
<span class="go">     a₄</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">l</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">a3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span> <span class="o">=</span> <span class="mi">1</span><span class="o">/</span><span class="p">(</span><span class="n">frac</span> <span class="o">-</span> <span class="n">a3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span> <span class="o">=</span> <span class="n">apart</span><span class="p">(</span><span class="n">frac</span><span class="p">,</span> <span class="n">a4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">frac</span>
<span class="go">a₄</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">l</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">a4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">list_to_frac</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
<span class="go">             1</span>
<span class="go">a₀ + ─────────────────</span>
<span class="go">               1</span>
<span class="go">     a₁ + ────────────</span>
<span class="go">                  1</span>
<span class="go">          a₂ + ───────</span>
<span class="go">                    1</span>
<span class="go">               a₃ + ──</span>
<span class="go">                    a₄</span>
</pre></div>
</div>
<aside class="sidebar">
<p class="sidebar-title">Quick Tip</p>
<p>You can execute multiple lines at once in SymPy Live.  Typing
<code class="docutils literal notranslate"><span class="pre">Shift-Enter</span></code> instead of <code class="docutils literal notranslate"><span class="pre">Enter</span></code> will enter a newline instead of
executing.</p>
</aside>
<p>Of course, this exercise seems pointless, because we already know that our
<code class="docutils literal notranslate"><span class="pre">frac</span></code> is <code class="docutils literal notranslate"><span class="pre">list_to_frac([a0,</span> <span class="pre">a1,</span> <span class="pre">a2,</span> <span class="pre">a3,</span> <span class="pre">a4])</span></code>.  So try the following
exercise.  Take a list of symbols and randomize them, and create the canceled
continued fraction, and see if you can reproduce the original list.  For
example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">random</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">l</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;a0:5&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">random</span><span class="o">.</span><span class="n">shuffle</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">orig_frac</span> <span class="o">=</span> <span class="n">frac</span> <span class="o">=</span> <span class="n">cancel</span><span class="p">(</span><span class="n">list_to_frac</span><span class="p">(</span><span class="n">l</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">l</span>
</pre></div>
</div>
<p>Click on “Run code block in SymPy Live” on the definition of <code class="docutils literal notranslate"><span class="pre">list_to_frac()</span></code>
above, and then on the above example, and try to reproduce <code class="docutils literal notranslate"><span class="pre">l</span></code> from
<code class="docutils literal notranslate"><span class="pre">frac</span></code>.  I have deleted <code class="docutils literal notranslate"><span class="pre">l</span></code> at the end to remove the temptation for
peeking (you can check your answer at the end by calling
<code class="docutils literal notranslate"><span class="pre">cancel(list_to_frac(l))</span></code> on the list that you generate at the end, and
comparing it to <code class="docutils literal notranslate"><span class="pre">orig_frac</span></code>.</p>
<p>See if you can think of a way to figure out what symbol to pass to <code class="docutils literal notranslate"><span class="pre">apart()</span></code>
at each stage (hint: think of what happens to <span class="math notranslate nohighlight">\(a_0\)</span> in the formula <span class="math notranslate nohighlight">\(a_0 +
\frac{1}{a_1 + \cdots}\)</span> when it is canceled).</p>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Simplification</a><ul>
<li><a class="reference internal" href="#simplify"><code class="docutils literal notranslate"><span class="pre">simplify</span></code></a></li>
<li><a class="reference internal" href="#polynomial-rational-function-simplification">Polynomial/Rational Function Simplification</a><ul>
<li><a class="reference internal" href="#expand">expand</a></li>
<li><a class="reference internal" href="#factor">factor</a></li>
<li><a class="reference internal" href="#collect">collect</a></li>
<li><a class="reference internal" href="#cancel">cancel</a></li>
<li><a class="reference internal" href="#apart">apart</a></li>
</ul>
</li>
<li><a class="reference internal" href="#trigonometric-simplification">Trigonometric Simplification</a><ul>
<li><a class="reference internal" href="#trigsimp">trigsimp</a></li>
<li><a class="reference internal" href="#expand-trig">expand_trig</a></li>
</ul>
</li>
<li><a class="reference internal" href="#powers">Powers</a><ul>
<li><a class="reference internal" href="#powsimp">powsimp</a></li>
<li><a class="reference internal" href="#expand-power-exp-expand-power-base">expand_power_exp / expand_power_base</a></li>
<li><a class="reference internal" href="#powdenest">powdenest</a></li>
</ul>
</li>
<li><a class="reference internal" href="#exponentials-and-logarithms">Exponentials and logarithms</a><ul>
<li><a class="reference internal" href="#expand-log">expand_log</a></li>
<li><a class="reference internal" href="#logcombine">logcombine</a></li>
</ul>
</li>
<li><a class="reference internal" href="#special-functions">Special Functions</a><ul>
<li><a class="reference internal" href="#rewrite">rewrite</a></li>
<li><a class="reference internal" href="#expand-func">expand_func</a></li>
<li><a class="reference internal" href="#hyperexpand">hyperexpand</a></li>
<li><a class="reference internal" href="#combsimp">combsimp</a></li>
<li><a class="reference internal" href="#gammasimp">gammasimp</a></li>
</ul>
</li>
<li><a class="reference internal" href="#example-continued-fractions">Example: Continued Fractions</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="printing.html"
                        title="previous chapter">Printing</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="calculus.html"
                        title="next chapter">Calculus</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/tutorial/simplification.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="calculus.html" title="Calculus"
             >next</a> |</li>
        <li class="right" >
          <a href="printing.html" title="Printing"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" >SymPy Tutorial</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Simplification</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/tutorial/simplification.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:23:56 GMT -->
</html>